/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */
package com.jav;

import com.jav.inter.Constantes;
import com.jav.per.PersisPedido;
import com.jav.per.Persistencia;
import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.impl.midp.VirtualKeyboard;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.table.DefaultTableModel;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.table.TableLayout;
import com.sun.lwuit.table.TableModel;
import com.sun.lwuit.util.Resources;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.rms.RecordEnumeration;

public class VentasXClientesContainer extends Container implements Constantes {

    private int totClientes;
    private long totVentas;

    public VentasXClientesContainer() {

        Resources res = null;
        try {
            res = Datos.getInstance().getRes();
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            //setLayout(new BorderLayout());
            //TableLayout layout = new TableLayout(1, 1);
            //setLayout(layout);

            String[] HEADER = new String[]{"CLIENTE", "VENTA"};
            String[][] datos = getDatosCliente();
            if (datos != null) {

                TableModel model = new DefaultTableModel(HEADER, datos, false);

                Table table = new Table(model) {

                    public boolean isCellEditable(int row, int col) {
                        return col != 0;
                    }
                };
                table.setScrollable(true);
                table.setIsScrollVisible(true);
                table.setIncludeHeader(true);

               /* Button LtotClientes = new Button(Long.toString(this.totClientes)) {

                    public void startTicker(long delay, boolean rightToLeft) {
                        super.startTicker(200, true);
                    }
                };
                Button LtotVentas = new Button(Long.toString(this.totVentas)) {

                    public void startTicker(long delay, boolean rightToLeft) {
                        super.startTicker(200, true);
                    }
                };*/

                //addComponent(BorderLayout.CENTER, table);
                addComponent(table);
                //addComponent(this.createPair("Total Clientes:", LtotClientes));
                //addComponent(this.createPair("Total Ventas:", LtotVentas));
                //Container c = new Container(new BoxLayout(BoxLayout.Y_AXIS));
                //c.addComponent(this.createPair("Total Clientes:", LtotClientes));
                //c.addComponent(this.createPair("Total Ventas:", LtotVentas));
                //addComponent(BorderLayout.SOUTH, c);

                


            }



        } catch (Exception e) {
            Datos.getInstance().addLogs(e.getMessage());
        } finally {
            res = null;
        }

    }

    private String[][] getDatosCliente() {


        Persistencia p = null;
        Hashtable clientes = null;
        String[][] datos = null;
        try {
            p = new Persistencia(PERS_PEDIDO);

            RecordEnumeration re = p.getRecordStore().enumerateRecords(null, null, false);
            while (re.hasNextElement()) {
                PersisPedido pd = new PersisPedido();
                pd.resurrect(re.nextRecord());
                if (clientes == null) {
                    clientes = new Hashtable();
                    clientes.put(pd.getCliente(), Long.toString(pd.getVenta()));
                    this.totClientes++;
                } else {
                    if (clientes.containsKey(pd.getCliente())) {
                        String venta = (String) clientes.get(pd.getCliente());
                        long lventa = Long.parseLong(venta) + pd.getVenta();
                        clientes.put(pd.getCliente(), Long.toString(lventa));

                    } else {
                        clientes.put(pd.getCliente(), Long.toString(pd.getVenta()));
                        this.totClientes++;
                    }

                }

                this.totVentas += pd.getVenta();
            }


        } catch (Exception ex) {
            Datos.getInstance().getLogs().append(ex.getMessage());
        } finally {
            try {
                p.close();
            } catch (Exception ex) {
            }
        }

        if (clientes != null) {

            datos = new String[clientes.size()+2][2];
            Enumeration e = clientes.keys();
            Object obj;
            int i = 0;
            while (e.hasMoreElements()) {
                obj = e.nextElement();
                datos[i][0] = obj.toString();
                datos[i][1] = clientes.get(obj).toString();
                i++;
            }


            datos[i][0] = "**Total\nClientes: ";
            datos[i][1] = Integer.toString(this.totClientes);


            datos[i+1][0] = "**Total\nVentas: ";
            datos[i+1][1] = Long.toString(this.totVentas);

        }

        return datos;


    }

    private Container createPair(String label, Component c) {
        Container pair = new Container(new BorderLayout());
        Label l = new Label(label);
        l.getStyle().setBgTransparency(100);
        pair.addComponent(BorderLayout.WEST, l);
        pair.addComponent(BorderLayout.CENTER, c);
        return pair;
    }

}
